//
//  RecommendViewController.m
//  GSG
//
//  Created by dllo on 15/4/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "RecommendViewController.h"//MainViewController.h
#import "RecommendCell.h"//MainTableViewCell.h
#import "MusicModel.h"//Model.h
#import "UIImageView+WebCache.h"
#import "AFNetworking.h"
#import "AFNetworkingTool.h"
#import "NewAlbumViewController.h"//NewVedioViewController.h
#import "SDCycleScrollView.h"//ScrollView三方
#import "NewSongPublishViewController.h"//新碟上架
#import "NewSongViewController.h"//新歌精选
#import "HotSongViewController.h"//热门歌单
#import "PersonalityViewController.h"//个性化推荐
#import"MBProgressHUD.h"
#import"PlayListViewController.h"
@interface RecommendViewController ()<UITableViewDataSource, UITableViewDelegate, SDCycleScrollViewDelegate>

@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UIPageControl *pageControl;
@property (nonatomic, retain) NSMutableArray *tableArray;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSArray *arrPoster;
@property (nonatomic,retain) NSMutableArray *dataArray;
@property (nonatomic,retain) NSMutableArray *dataArray1;
@property (nonatomic,retain) NSMutableArray *dataArray2;
@property (nonatomic, retain) NSMutableArray *arraySection;
@property (nonatomic,retain) NSMutableArray *arrURL;
@property (nonatomic, retain) UIImageView *imageview;
@property (nonatomic, copy)NSString *SongValue0;
@property (nonatomic, copy)NSString *SongValue1;
@property (nonatomic, copy)NSString *SongValue2;
@property (nonatomic, copy)NSString *PersonalityValue0;
@property (nonatomic, copy)NSString *PersonalityValue1;
@property (nonatomic, copy)NSString *PersonalityValue2;
@property (nonatomic, copy)NSString *PersonalityValue3;
@property (nonatomic, copy)NSString *PersonalityValue4;
@property (nonatomic, copy)NSString *PersonalityValue5;
@property (nonatomic, copy)NSString *PersonalityValue6;
@property (nonatomic, copy)NSString *PersonalityValue7;
@property (nonatomic, copy)NSString *PersonalityValue8;
@property (nonatomic, copy)NSString *PersonalityValue9;


@end

@implementation RecommendViewController

- (void)dealloc
{
    [_tableView release];
    [_arrPoster release];
    [_dataArray release];
    [_dataArray1 release];
    [_dataArray2 release];
    [_arraySection release];
    [_arrURL release];
    [_imageview release];
    [_tableArray release];
    [_scrollView release];
    [_pageControl release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSMutableArray array];
        self.arraySection = [NSMutableArray arrayWithObjects:@"新歌首发", @"热门歌单", @"个性化推荐", nil];
    }
    return self;
}
#pragma mark -
#pragma mark 创建TableView
- (void)createTableView
{
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bf.jpg"]];

    // 网络加载 --- 创建带标题的图片轮播器
    SDCycleScrollView*cycleScrollView2 = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height * 1 / 3 - (self.view.frame.size.height * 40 / 667)) imageURLsGroup:nil];
    
    cycleScrollView2.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
    cycleScrollView2.delegate=self;
    cycleScrollView2.dotColor = [UIColor whiteColor]; // 自定义分页控件小圆标颜色
    
    [self.view addSubview:cycleScrollView2];
    cycleScrollView2.autoScrollTimeInterval = 4.0;
    cycleScrollView2.backgroundColor=[UIColor blackColor];
    
    //4.0 影响播放的效果 可以改变数字
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        cycleScrollView2.imageURLsGroup = self.arrURL;
    });
    
   
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-self.view.frame.size.height * 40 / 667) style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.tableHeaderView=cycleScrollView2;
    self.tableView.rowHeight = 100;
    self.tableView.tableHeaderView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"zhanweitu.jpg"]];
//    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //注册
    [self.tableView registerClass:[RecommendCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.tableView];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView release];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.arraySection.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.tableArray.count == 0) {
        return 0;
    } else {
        if (section==0) {
            return 2;
        }
        if (section==1) {
            return 3;
        }
        if (section==2) {
            return 10;
        }
        return 0;
    }
}

- ( NSString *)tableView:( UITableView *)tableView titleForHeaderInSection:( NSInteger )section
{
    return self.arraySection[section];
}

#pragma mark -
#pragma mark 协议方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RecommendCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.section == 0) {
        MusicModel *model = [[MusicModel alloc]init];
        [model setValuesForKeysWithDictionary:self.dataArray[indexPath.row]];
        cell.aLabel.text = model.name;
        cell.tLabel.text = model.desc;
        NSURL *url = [NSURL URLWithString:model.pic_url];
        [cell.image sd_setImageWithURL:url];
    }
    if (indexPath.section == 1)
    {
        MusicModel *model = [[MusicModel alloc]init];
        [model setValuesForKeysWithDictionary:self.dataArray1[indexPath.row]];
        cell.aLabel.text = model.name;
        cell.tLabel.text = model.desc;
        NSURL *url = [NSURL URLWithString:model.pic_url];
        [cell.image sd_setImageWithURL:url];
    }
    if (indexPath.section==2) {
        MusicModel *model = [[MusicModel alloc]init];
        [model setValuesForKeysWithDictionary:self.dataArray2[indexPath.row]];
        cell.aLabel.text = model.name;
        cell.tLabel.text = model.desc;
        NSURL *url = [NSURL URLWithString:model.pic_url];
        [cell.image sd_setImageWithURL:url];
    }
    return cell;
}

#pragma mark -
#pragma mark cell点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0 && indexPath.row == 0) {
        NewSongViewController *NewSong = [[[NewSongViewController alloc] init] autorelease];
        [self.navigationController pushViewController:NewSong animated:YES];
        
    } else if (indexPath.section == 0 && indexPath.row == 1) {
        NewSongPublishViewController *NewAlbum = [[[NewSongPublishViewController alloc] init] autorelease];
        [self.navigationController pushViewController:NewAlbum animated:YES];
        
    } else if (indexPath.section == 1 && indexPath.row == 0) {
        HotSongViewController *song = [[[HotSongViewController alloc] init] autorelease];
        song.Songstr = self.SongValue0;
        [self.navigationController pushViewController:song animated:YES];
        
        
    } else if (indexPath.section == 1 && indexPath.row == 1) {
        HotSongViewController *song = [[[HotSongViewController alloc] init] autorelease];
        song.Songstr = self.SongValue1;
        [self.navigationController pushViewController:song animated:YES];
        
    } else if (indexPath.section == 1 && indexPath.row == 2) {
        HotSongViewController *song = [[[HotSongViewController alloc] init] autorelease];
        song.Songstr = self.SongValue2;
        [self.navigationController pushViewController:song animated:YES];
        
    } else if (indexPath.section == 2 && indexPath.row == 0) {
        PersonalityViewController *personality = [[[PersonalityViewController alloc] init] autorelease];
        personality.PersonalityStr = self.PersonalityValue0;
//        NSLog(@"%@",self.PersonalityValue0);
        [self.navigationController pushViewController:personality animated:YES];
        
    } else if (indexPath.section == 2 && indexPath.row == 1) {
        PersonalityViewController *personality = [[[PersonalityViewController alloc] init] autorelease];
        personality.PersonalityStr = self.PersonalityValue1;
        [self.navigationController pushViewController:personality animated:YES];
        
    } else if (indexPath.section == 2 && indexPath.row == 2) {
        PersonalityViewController *personality = [[[PersonalityViewController alloc] init] autorelease];
         personality.PersonalityStr = self.PersonalityValue2;
        [self.navigationController pushViewController:personality animated:YES];
        
    } else if (indexPath.section == 2 && indexPath.row == 3) {
        PersonalityViewController *personality = [[[PersonalityViewController alloc] init] autorelease];
        personality.PersonalityStr = self.PersonalityValue3;
        [self.navigationController pushViewController:personality animated:YES];
        
    } else if (indexPath.section == 2 && indexPath.row == 4) {
        PersonalityViewController *personality = [[[PersonalityViewController alloc] init] autorelease];
        personality.PersonalityStr = self.PersonalityValue4;
        [self.navigationController pushViewController:personality animated:YES];
        
    } else if (indexPath.section == 2 && indexPath.row == 5) {
        PersonalityViewController *personality = [[[PersonalityViewController alloc] init] autorelease];
        personality.PersonalityStr = self.PersonalityValue5;
        [self.navigationController pushViewController:personality animated:YES];
        
    } else if (indexPath.section == 2 && indexPath.row == 6) {
        PersonalityViewController *personality = [[[PersonalityViewController alloc] init] autorelease];
        personality.PersonalityStr = self.PersonalityValue6;
        [self.navigationController pushViewController:personality animated:YES];
        
    } else if (indexPath.section == 2 && indexPath.row == 7) {
        PersonalityViewController *personality = [[[PersonalityViewController alloc] init] autorelease];
        personality.PersonalityStr = self.PersonalityValue7;
        [self.navigationController pushViewController:personality animated:YES];
        
    } else if (indexPath.section == 2 && indexPath.row == 8) {
        PersonalityViewController *personality = [[[PersonalityViewController alloc] init] autorelease];
        personality.PersonalityStr = self.PersonalityValue8;
        [self.navigationController pushViewController:personality animated:YES];
        
    } else if (indexPath.section == 2 && indexPath.row == 9) {
        PersonalityViewController *personality = [[[PersonalityViewController alloc] init] autorelease];
        personality.PersonalityStr = self.PersonalityValue9;
        [self.navigationController pushViewController:personality animated:YES];
    }
}

#pragma mark -
#pragma mark Cell解析
- (void)blockAsynGET
{
    
    [AFNetworkingTool NetWorkingTooStr:@"http://online.dongting.com/recomm/recomm_modules?&app=ttpod&v=v7.7.0.2015012818&uid=&mid=iPhone4&f=f320&s=s310&imsi=&hid=&splus=7.1.1&active=1&net=2&openudid=ad6f50f586818c4f36ff4358053c1b53fee58cdf&idfa=E2EAC64B-1F1C-4AC5-A0E1-8BE37A03FB96&utdid=VTCyBQSkYzwDAALO9zZTFfPC&alf=201200&version=1429340403" block:^(id result) {
        
        
        NSMutableDictionary *dictionary = result;
        NSMutableDictionary *datadic = [dictionary objectForKey:@"data"];
        NSMutableArray *songlistsArray = [datadic objectForKey:@"songlists"];
        //新歌
        NSMutableDictionary *dictionary1 = [songlistsArray objectAtIndex:0];
        //热门歌单
        NSMutableDictionary *dictionary2 = [songlistsArray objectAtIndex:1];
        //电台
        NSMutableDictionary *dictionary3 = [songlistsArray objectAtIndex:7];
        
        self.dataArray = [dictionary1 objectForKey:@"data"];
        //************************************************//
        self.dataArray1 = [dictionary2 objectForKey:@"data"];
        //热门歌单【0】
        NSMutableDictionary *SongDictionary1  = self.dataArray1[0];
        NSMutableDictionary *songdic1 = [SongDictionary1 objectForKey:@"action"];
        self.SongValue0 = [songdic1 objectForKey:@"value"];
        //热门歌单【1】
        NSMutableDictionary *SongDictionary2 = self.dataArray1[1];
        NSMutableDictionary *songdic2 = [SongDictionary2 objectForKey:@"action"];
        self.SongValue1 = [songdic2 objectForKey:@"value"];
        //热门歌单【2】
        NSMutableDictionary *SongDictionary3  = self.dataArray1[2];
        NSMutableDictionary *songdic3 = [SongDictionary3 objectForKey:@"action"];
        self.SongValue2 = [songdic3 objectForKey:@"value"];
    
        //************************************************//
        self.dataArray2 = [dictionary3 objectForKey:@"data"];
        //个性化推荐【0】
        NSMutableDictionary *PersonDictionary0 = self.dataArray2[0];
        NSMutableDictionary *personality0 = [PersonDictionary0 objectForKey:@"action"];
        self.PersonalityValue0 = [personality0 objectForKey:@"value"];
        //个性化推荐【1】
        NSMutableDictionary *PersonDictionary1 = self.dataArray2[1];
        NSMutableDictionary *personality1 = [PersonDictionary1 objectForKey:@"action"];
        self.PersonalityValue1 = [personality1 objectForKey:@"value"];
        //个性化推荐【2】
        NSMutableDictionary *PersonDictionary2 = self.dataArray2[2];
        NSMutableDictionary *personality2 = [PersonDictionary2 objectForKey:@"action"];
        self.PersonalityValue2 = [personality2 objectForKey:@"value"];
        //个性化推荐【3】
        NSMutableDictionary *PersonDictionary3 = self.dataArray2[3];
        NSMutableDictionary *personality3 = [PersonDictionary3 objectForKey:@"action"];
        self.PersonalityValue3 = [personality3 objectForKey:@"value"];
        //个性化推荐【4】
        NSMutableDictionary *PersonDictionary4 = self.dataArray2[4];
        NSMutableDictionary *personality4 = [PersonDictionary4 objectForKey:@"action"];
        self.PersonalityValue4 = [personality4 objectForKey:@"value"];
        //个性化推荐【5】
        NSMutableDictionary *PersonDictionary5 = self.dataArray2[5];
        NSMutableDictionary *personality5 = [PersonDictionary5 objectForKey:@"action"];
        self.PersonalityValue5 = [personality5 objectForKey:@"value"];
        //个性化推荐【6】
        NSMutableDictionary *PersonDictionary6 = self.dataArray2[6];
        NSMutableDictionary *personality6 = [PersonDictionary6 objectForKey:@"action"];
        self.PersonalityValue6 = [personality6 objectForKey:@"value"];
        //个性化推荐【7】
        NSMutableDictionary *PersonDictionary7 = self.dataArray2[7];
        NSMutableDictionary *personality7 = [PersonDictionary7 objectForKey:@"action"];
        self.PersonalityValue7 = [personality7 objectForKey:@"value"];
        //个性化推荐【8】
        NSMutableDictionary *PersonDictionary8 = self.dataArray2[8];
        NSMutableDictionary *personality8 = [PersonDictionary8 objectForKey:@"action"];
        self.PersonalityValue8 = [personality8 objectForKey:@"value"];
        //个性化推荐【9】
        NSMutableDictionary *PersonDictionary9 = self.dataArray2[9];
        NSMutableDictionary *personality9 = [PersonDictionary9 objectForKey:@"action"];
        self.PersonalityValue9 = [personality9 objectForKey:@"value"];


        for (int i = 0; i < [self.dataArray1 count]; i++) {
            [self.dataArray addObject:self.dataArray1[i]];
        }
        
        for (int i = 0; i < [self.dataArray2 count]; i++) {
            [self.dataArray addObject:self.dataArray2[i]];
        }
        
        for (int i = 0; i < [self.dataArray count]; i++) {
            NSMutableDictionary *dic = self.dataArray[i];
            MusicModel *model = [[[MusicModel alloc] init] autorelease];
            [model setValuesForKeysWithDictionary:dic];
            [self.tableArray addObject:model];
        }
        NSArray*arrPosters=[dictionary objectForKey:@"posters"];
        NSDictionary*dic1=arrPosters[0];
        self.arrPoster=[dic1 objectForKey:@"data"];
        
        for(int i=0;i<self.arrPoster.count ;i++)
        {
            self.arrURL=[[NSMutableArray alloc]init];
            NSString *str=[self.arrPoster[i] objectForKey:@"pic_url"];
            NSURL *url = [NSURL URLWithString:str];
            [self.arrURL  addObject:url ];
//            NSLog(@"++++++%@",self.arrURL);
        }
        
        [self.tableView reloadData];
    }];
    
}

#pragma mark -
#pragma mark ScrollView解析
- (void)blockAsndGet
{
    NSString *urlRecommendPoster = @"http://online.dongting.com/recomm/recomm_modules?&app=ttpod&v=v7.7.0.2015012818&uid=&mid=iPhone4&f=f320&s=s310&imsi=&hid=&splus=7.1.1&active=1&net=2&openudid=ad6f50f586818c4f36ff4358053c1b53fee58cdf&idfa=E2EAC64B-1F1C-4AC5-A0E1-8BE37A03FB96&utdid=VTCyBQSkYzwDAALO9zZTFfPC&alf=201200&version=1429340403";
    [ AFNetworkingTool NetWorkingTooStr:urlRecommendPoster block:^(id result) {
        NSDictionary *dic = [result objectForKey:@"data"];
        NSArray *arrPosters = [dic objectForKey:@"posters"];
        NSDictionary *dic1 = arrPosters[0];
        NSMutableArray *dataArr = [dic1 objectForKey:@"data"];
        
        self.arrURL = [[NSMutableArray alloc] init];
         [_HUD hide:YES afterDelay:2];
        for (NSMutableDictionary *dic in dataArr) {
            NSString *picurl = [dic objectForKey:@"pic_url"];
            NSURL *url = [NSURL URLWithString:picurl];
            [self.arrURL addObject:url];
        }}
];
    

   
}

#pragma mark -
#pragma mark NSTimer点击事件
-(void)change
{
    NSInteger dx = self.scrollView.contentOffset.x;
    NSInteger pageNume = dx / 300;
    if (pageNume != 4) {
        self.scrollView.contentOffset = CGPointMake(dx+self.scrollView.frame.size.width, 0);
    } else {
        self.scrollView.contentOffset=CGPointMake(300, 0);
    }
}
-(void)actionBuLand{
    PlayListViewController *plvc=[[PlayListViewController alloc]init];
    [self.navigationController pushViewController:plvc animated:YES];
    [plvc release];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"播放" style:UIBarButtonItemStyleDone target:self action:@selector(actionBuLand)];
 self.navigationItem.rightBarButtonItem.tintColor=[UIColor whiteColor];
    self.navigationItem.title = @"推荐";
    
    //    [self createSrollView];
    //    [self createPageControl];
    [self createTableView];
    [self blockAsndGet];
    [self blockAsynGET];
    
    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(change) userInfo:nil repeats:YES];

    //方式1.直接在View上show
    _HUD = [[MBProgressHUD showHUDAddedTo:self.view animated:YES] retain];

    
    //常用的设置
    //小矩形的背景色
    _HUD.color = [UIColor clearColor];//这儿表示无背景
    //显示的文字
    _HUD.labelText = @"提示";
    //细节文字
    _HUD.detailsLabelText = @"正在加载内容";
    //是否有庶罩
    _HUD.dimBackground = YES;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
